/*
 * Copyright (c) 2023-2025 elsfs Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.elsfs.cloud.common.mybatis.ext.sql;

import java.util.function.Consumer;
import org.elsfs.cloud.common.core.entity.BaseEntity;
import org.elsfs.cloud.common.mybatis.ext.sql.support.builder.StandardSelectSql;

/**
 * 查询SQL
 *
 * @param <C> 子类具体类型
 * @param <H> Having条件构建器类型
 * @param <S> 选择条件构建器类型
 */
public interface SelectSql<
        C extends SelectSql<C, S, H>,
        S extends SelectSql.SelectConditionBuilder<S, H, C>,
        H extends SelectCondition.HavingConditionBuilder<H, S>>
    extends Selectable<C>, SqlFragment {

  /**
   * 设置查询表
   *
   * @param entityClass 实体类
   * @return 具体实现
   */
  C from(Class<? extends BaseEntity> entityClass);

  /**
   * 设置查询表
   *
   * @param table 表名
   * @return 具体实现
   */
  C from(String table);

  /**
   * 设置查询表
   *
   * @param entityClass 实体类
   * @param tableAlias 表别名
   * @return 具体实现
   */
  C from(Class<? extends BaseEntity> entityClass, String tableAlias);

  /**
   * 设置查询表
   *
   * @param table 表名
   * @param tableAlias 表别名
   * @return 具体实现
   */
  C from(String table, String tableAlias);

  /**
   * 设置子查询表
   *
   * @param sqlBuilderConsumer SQL构建器消费器
   * @param tableAlias 表别名
   * @return 具体实现
   */
  C from(Consumer<StandardSelectSql> sqlBuilderConsumer, String tableAlias);

  /**
   * 设置Where条件
   *
   * @return 条件构建器
   */
  SelectConditionBuilder<S, H, C> where();

  /**
   * 查询条件构建器
   *
   * @param <C> 具体实现类型
   * @param <H> 分组条件构建器类型
   * @param <P> 上级对象类型
   */
  interface SelectConditionBuilder<
          C extends SelectConditionBuilder<C, H, P>,
          H extends SelectCondition.HavingConditionBuilder<H, C>,
          P extends SelectSql<P, C, H>>
      extends SelectCondition<C, H>, Completable<P> {}
}
